package com.webank.weideagroup2.web.interceptor;

import com.webank.weideagroup2.bean.Platform;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Create Time: 2018年07月27日 10:16
 *
 * @author fin
 */
public class BackendInterceptor implements HandlerInterceptor {

    //在请求处理之前进行调用（Controller方法调用之前
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        Platform platform;
        platform = new Platform();
        platform.setPlatform("finyu保险");
        platform.setType(2);
        platform.setUserName("finyu");
        httpServletRequest.getSession().setAttribute("_platform", platform);
        return true;


    }

    //请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    //在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

    private boolean isPublicPage(HandlerMethod handlerMethod) {
        if (handlerMethod == null) {
            return false;
        } else {
            PublicPage pp = handlerMethod.getMethodAnnotation(PublicPage.class);
            if (pp != null) {
                return true;
            } else {
                Class<?> clazz = handlerMethod.getMethod().getDeclaringClass();
                pp = AnnotationUtils.findAnnotation(clazz, PublicPage.class);
                return pp != null;
            }
        }
    }
}